package com.example.springbootdemo.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @Author: ShengXiaJia
 * @Date: 2018/12/18 19:24
 * @Description: 日期时间工具类
 */
public class DateUtil {

    /**
     * 根据当天日期获取本周的日期,格式yyyyMMdd
     **/
    public static List<String> getTimeInterval(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        List<String> list = new ArrayList<>();
        list.add(sdf.format(cal.getTime()));
        for (int i = 0; i < 6; i++) {
            cal.add(Calendar.DATE, 1);
            list.add(sdf.format(cal.getTime()));
        }
        return list;
    }

    /**
     * 根据传入的起始日期和终止日期获取日期的list
     **/
    public static List<String> getBetweenDate(String startTime, String endTime) {
        // 返回的日期集合
        List<String> days = new ArrayList<String>();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date start = dateFormat.parse(startTime);
            Date end = dateFormat.parse(endTime);

            Calendar tempStart = Calendar.getInstance();
            tempStart.setTime(start);

            Calendar tempEnd = Calendar.getInstance();
            tempEnd.setTime(end);
            tempEnd.add(Calendar.DATE, +1);// 日期加1(包含结束)
            while (tempStart.before(tempEnd)) {
                days.add(dateFormat.format(tempStart.getTime()));
                tempStart.add(Calendar.DAY_OF_YEAR, 1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return days;
    }

    /**
     * 根据年月日返回星期几
     *
     * @param date yyyyMMDD
     * @return String //返回值直接返回星期几
     */
    public static String weekByDate(String date) {
        String str = "";
        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");
        Date d = null;
        try {
            d = fmt.parse(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(d);
        int weekDay = cal.get(Calendar.DAY_OF_WEEK);
        switch (weekDay) {
            case 1:
                str = "周日";
                break;
            case 2:
                str = "周一";
                break;
            case 3:
                str = "周二";
                break;
            case 4:
                str = "周三";
                break;
            case 5:
                str = "周四";
                break;
            case 6:
                str = "周五";
                break;
            case 7:
                str = "周六";
                break;
            default:
                break;
        }

        return str;
    }

    /**
     * 获取今日日期yyyy-MM-dd
     **/
    public static String getToday() {
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        return sim.format(new Date());
    }

    /**
     * 获取昨日日期yyyy-MM-dd
     **/
    public static String getYesterday() {
        Calendar calendar = Calendar.getInstance();//此时打印它获取的是系统当前时间
        calendar.add(Calendar.DATE, -1);    //得到前一天
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        return sim.format(calendar.getTime());
    }

    /**
     * 获取本周日期yyyy-MM-dd
     **/
    public static List<String> getThisWeekDay() {
        Calendar cal = Calendar.getInstance();
        // 判断要计算的日期是否是周日，如果是则减一天计算周六的，否则会出问题，计算到下一周去了
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);// 获得当前日期是一个星期的第几天
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> list = new ArrayList<>();
        list.add(sdf.format(cal.getTime()));
        list.add(sdf.format(new Date()));
        return list;
    }

    /**
     * 获取上周日期yyyy-MM-dd
     **/
    public static List<String> getLastWeekDay() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -7);
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        if (1 == dayWeek) {
            cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        int day = cal.get(Calendar.DAY_OF_WEEK);
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> list = new ArrayList<>();
        list.add(sdf.format(cal.getTime()));
        cal.add(Calendar.DATE, 6);
        list.add(sdf.format(cal.getTime()));
        return list;
    }

    /**
     * 获取本月日期yyyy-MM-dd
     **/
    public static List<String> getThisMonthDay() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -(cal.get(Calendar.DAY_OF_MONTH) - 1));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<String> list = new ArrayList<>();
        list.add(sdf.format(cal.getTime()));
        list.add(sdf.format(new Date()));
        return list;
    }

    /**
     * 获取上月日期yyyy-MM-dd
     **/
    public static List<String> getLastMonthDay() {
        List<String> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -cal.get(Calendar.DAY_OF_MONTH));
        String endtime = sdf.format(cal.getTime());
        cal.add(Calendar.DAY_OF_MONTH, -(cal.get(Calendar.DAY_OF_MONTH) - 1));
        String starttime = sdf.format(cal.getTime());
        list.add(starttime);
        list.add(endtime);
        return list;
    }

    /**
     * 获取最近7天日期yyyy-MM-dd
     **/
    public static List<String> getNear7Day() {
        List<String> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -6);
        String starttime = sdf.format(cal.getTime());
        list.add(starttime);
        list.add(sdf.format(new Date()));
        return list;
    }

    /**
     * 获取最近30天日期yyyy-MM-dd
     **/
    public static List<String> getNear30Day() {
        List<String> list = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, -29);
        String starttime = sdf.format(cal.getTime());
        list.add(starttime);
        list.add(sdf.format(new Date()));
        return list;
    }
}
